/**
 * app登录
 * @param {string} phone 手机号
 * @param {string} captcha 验证码
 * @returns {object} 登录结果
 * 
 * 返回示例：
 * {
    "code": 200,
    "msg": "ok",
    "data": {
        "isLogin": true,
        "loginDeviceType": "DEF",
        "loginId": "18571858721",
        "loginType": "login",
        "sessionTimeout": 2591999,
        "tokenActiveTimeout": -1,
        "tokenName": "satoken",
        "tokenSessionTimeout": -2,
        "tokenTimeout": 2591999,
        "tokenValue": "9cfdce21-bce5-4dfc-a7fe-d018b5a7318d"
    }
}
 */
function login(phone, captcha) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/app/login',
			data: {
				phone,
				captcha
			},
			method: 'POST'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

function sendCaptcha(phone) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/app/captcha/send?phone=' + phone,
			data: {},
			method: 'GET'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

function getDeviceList(phone) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/app/device/list/' + phone,
			data: {},
			method: 'POST'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve([])
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

function getDeviceInfo(id) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/wx/getDeviceInfo/' + id,
			data: {},
			method: 'GET'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

function editNickname(phone, nickname) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/app/user/edit',
			data: {
				phone,
				nickname
			},
			method: 'POST'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

function getChatMsg(deviceId, page, size) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + `/api/wx/chatMsg?deviceName=${deviceId}&page=${page}&size=${size}`,
			method: 'GET'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve([])
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

function getVoices(platform, deviceId) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + `/api/wx/voice?platform=${platform}&deviceId=${deviceId}`,
			method: 'GET'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve([])
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

export function editVoice(voiceId, voiceName, voiceDesc) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/wx/cloneVoice/edit',
			data: {
				voiceId,
				voiceName,
				voiceDesc
			},
			method: 'POST'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

/**
 * 检查克隆语音额度是否存在
 * @param {*} deviceId 
 * @param {*} phone 
 * @returns 
 */
export function checkCloneVoiceOrder(deviceId, phone) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/app/cloneVoiceOrder/check?deviceId=' + deviceId + '&phone=' + phone,
			method: 'GET'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve(false)
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

/**
 * 创建克隆语音订单
 *
 * @param {*} deviceId 
 * @param {*} userId 
 * @returns 
 */
export function createCloneVoiceOrder(deviceId, userId) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/app/cloneVoiceOrder/create',
			data: {
				deviceId,
				userId
			},
			method: 'POST'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

/**
 * 付款克隆语音订单
 * @param {Object} deviceId
 * @param {Object} userId
 */
export function payCloneVoiceOrder(orderId, payType) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/app/cloneVoiceOrder/pay',
			data: {
				orderId,
				payType
			},
			method: 'POST'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

function createSubscribeOrder(deviceId, userId, amount, type) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/app/subscribeOrder/create',
			data: {
				deviceId,
				userId,
				amount,
				type
			},
			method: 'POST'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}
function paySubscribeOrder(orderId, payType) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/app/subscribeOrder/pay',
			data: {
				orderId,
				payType
			},
			method: 'POST'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

function getBots(platform, accountId) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + `/api/wx/bot?platform=${platform}&accountId=${accountId}`,
			method: 'GET'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve([])
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

/**
 * 获取设备类型
 * @param {string} deviceId 
 * @returns 
 */
export function getDeviceType(deviceId) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/wx/device/type?deviceId=' + deviceId,
			method: 'GET'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve("")
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

function bindUserDevice(phone, deviceId, ssid, mac, networkType) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/wx/bindUserDevice',
			data: {
				phone,
				deviceId,
				ssid,
				mac,
				networkType
			},
			method: 'POST'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

function saveWifi(phone, ssid, password) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/app/wifi/my',
			data: {
				phone,
				wifiName: ssid,
				wifiPwd: password
			},
			method: 'POST'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

function getMyWifiList(phone) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/app/wifi/my?phone=' + phone,
			method: 'GET'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

function pushMsgToDevice(deviceId, msg) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/app/device/push/msg',
			data: {
				deviceId,
				msg
			},
			method: 'POST'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

function checkPushMsgStatus(pushId) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/app/device/pushMsg?id=' + pushId,
			method: 'GET'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

function saveDeviceRole(data) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/app/device/role',
			data,
			method: 'POST'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}
function playVoice(voiceId, isClone) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${getApp().globalData.apiHost}/api/app/voice/play?voiceId=${voiceId}&isClone=${isClone}`,
			method: 'GET'
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve("")
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

function uploadFile(fileUrl, userId) {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: getApp().globalData.apiHost + '/api/app/file/upload',
			filePath: fileUrl,
			name: 'file',
			formData: {
				userId
			}
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}

function getPayScheme(orderId, business, envVersion) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.apiHost + '/api/app/wx/pay/scheme',
			method: 'POST',
			data: {
				orderId,
				business,
				envVersion
			}
		}).then(res => {
			let result = res.data
			if (result) {
				resolve(result)
			} else {
				resolve({})
			}
		}).catch(error => {
			console.error(error)
			reject(error)
		})
	})
}


export default {
    login,
	sendCaptcha,
	getDeviceList,
	getDeviceInfo,
	editNickname,
	getChatMsg,
	getVoices,
	editVoice,
	checkCloneVoiceOrder,
	createCloneVoiceOrder,
	payCloneVoiceOrder,
	createSubscribeOrder,
	paySubscribeOrder,
	getBots,
	getDeviceType,
	bindUserDevice,
	saveWifi,
	getMyWifiList,
	pushMsgToDevice,
	checkPushMsgStatus,
	saveDeviceRole,
	playVoice,
	uploadFile,
	getPayScheme
}